﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace ZY7.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UploadFilesController : ControllerBase
    {
        public UploadFilesController(IWebHostEnvironment env)
        {
            Env = env;
        }

        public IWebHostEnvironment Env { get; }

        /// <summary>
        ///  上传文件
        /// </summary>
        /// <param name="file">文件对象</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Upload(IFormFile file) 
        {

            string newfilename = Guid.NewGuid().ToString(); //DateTime.Now.ToString("yyyyMMddHHmmss");
            //找到保存文件位置
            string path=Env.WebRootPath + @"\pic\"+file.FileName;
            using (FileStream fs=System.IO.File.Create(path))//文件完整路径 
            {
                file.CopyTo(fs);//把要上传的文件，装到fs里面
                fs.Close();
            }//内存释放
            return Ok("/pic/"+file.FileName);
        }
    }
}
